<?php

namespace App\Http\ViewComposers;

use App\Handlers\role;
use Illuminate\View\View;
use App\Services\Config\ChannelsService;
use Illuminate\Support\Facades\Auth;

class ChannelRadioAllComposer
{
    public function __construct(ChannelsService $channelsService)
    {
        $this->channelsService = $channelsService;
    }

    public function compose(View $view)
    {
        $reqGameId = ($view->reqGameId == null ? old('game_id') : $view->reqGameId);
        $reqChannelId = ($view->reqChannelId == null ? old('channel_id') : $view->reqChannelId);
        $channels = $this->channelsService->channelsRepository->all();

        //拼出 渠道table
        $channelHtml = '';
        foreach ($channels as $channel) {
            $checked = '';
            $show = 'display:none;';

            if ($reqGameId == $channel['game_id'] && $channel['channel_id'] == $reqChannelId) {
                $checked = 'checked="checked"';
            }

            if ($reqGameId == $channel['game_id']) {
                $show = 'display:block;';
            }

            $channelHtml .= <<<EOF
<div class="col-sm-1" style="min-width: 20rem;{$show}" data-game-id="{$channel['game_id']}">
    <label>&emsp;{$channel['channel_name']}
            <input type="radio" class="pull-left" $checked name="channel_id" value="{$channel['channel_id']}">
    </label>
</div>
EOF;
        }

        $view->with(compact('channelHtml'));
    }
}